from playwright.sync_api import sync_playwright
import json

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, args=['--start-maximized'],slow_mo=1000)
    context = browser.new_context()
    page = context.new_page()
    page.goto("http://172.28.1.3:2460/#/")
    # 获取账号文本框元素信息
    account = page.get_by_placeholder("您的账号")
    print("账号: ", account.bounding_box())
    # 获取密码文本框元素信息
    password = page.get_by_placeholder("请输入您的密码")
    print("密码: ", password.bounding_box())
    # 获取登录按钮元素信息
    button = page.get_by_role("button")
    print("登录按钮: ", button.bounding_box())

    # 输入账号和密码
    page.get_by_placeholder("您的账号").type("kuanglei", delay=30)
    page.get_by_placeholder("请输入您的密码").fill("123456")
    # 点击登录
    page.get_by_role("button").click()
    page.wait_for_load_state("networkidle")
    # 保存storage state 到指定的文件
    storage = context.storage_state(path="state.json")
    # 打开并读取JSON文件
    with open('state.json', 'r', encoding='utf-8') as file:
        data = json.load(file)
    # 打印JSON数据
    print(data)

    context.close()
    browser.close()